package login1;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.sql.DataSource;
import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

@WebServlet("/login1")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html; charset=utf8");
        //从客户端请求中获取用户名和密码
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        //判定账户密码是否正确
        //这里假设已经注册的账户为:
        //username:zhangsan  password:123
        if (!"zhangsan".equals(username) || !"123".equals(password)) {
            resp.getWriter().write("很抱歉，登录失败，您的用户名或密码输入错误!");
            return;
        }
        //登录成功
        System.out.println("" + username + "成功登录");
        //从数据库中获取该用户的登录次数
        int loginCount = 0;
        try {
            loginCount = load(username);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        //由于登录成功，所以登录次数加一，保存到数据库中
        try {
            save(username, String.valueOf(++loginCount));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        //设置Session
        //getSession的参数true表示若查找不到HttpSession,就会新建立一个，并生成
        //一个sessionId插入哈希表,通过Set-Cookie返回给浏览器
        HttpSession session = req.getSession(true);
        //HttpSession对象自身相当于一个哈希表，可以根据需求设置里面的参数
        session.setAttribute("username", "zhangsan");
        session.setAttribute("loginCount", loginCount);
        //设置重定向到主界面index
        resp.sendRedirect("index1");
    }

    //修改数据库中的数据(登录次数)
    private void save(String username, String loginCount) throws SQLException {
        //创建数据源
        DataSource dataSource = SingletonLazy.getDataSource();
        //建立连接
        Connection connection = (Connection) dataSource.getConnection();
        //构造sql
        String sql = "update message set loginCount = ? where name = ?";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setString(1, loginCount);
        statement.setString(2, username);
        //执行sql
        int ret = statement.executeUpdate();
        //打印日志
        System.out.println("ret = " + ret);
        //关闭数据库
        statement.close();
        connection.close();
    }

    //从数据库中获取登录次数
    private int load(String username) throws SQLException {
        //创建数据源
        DataSource dataSource = SingletonLazy.getDataSource();//不嫌麻烦，你就来攻击~
        //建立连接
        Connection connection = (Connection) dataSource.getConnection();
        //构造sql
        String sql = "select loginCount from message where name = ?";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setString(1, username);
        //执行sql
        ResultSet resultSet = statement.executeQuery();
        int loginCount = 0;
        while(resultSet.next()) {//这里只对应一个结果
            loginCount = resultSet.getInt("loginCount");
        }
        //关闭数据库
        statement.close();
        connection.close();
        return loginCount;
    }
}
